package com.heima.common.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;

/*
*
* 描述：将布尔值转换为整数
* */
public class BooleanToIntegerDeserializer extends JsonDeserializer<Integer> {
    @Override
    public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        switch (p.getCurrentToken()) {
            case VALUE_TRUE:
                return 1;
            case VALUE_FALSE:
                return 0;
            case VALUE_NUMBER_INT:
                return p.getValueAsInt();
            case VALUE_STRING:
                String value = p.getValueAsString();
                if ("true".equalsIgnoreCase(value)) {
                    return 1;
                } else if ("false".equalsIgnoreCase(value)) {
                    return 0;
                } else {
                    try {
                        return Integer.parseInt(value);
                    } catch (NumberFormatException e) {
                        return 0;
                    }
                }
            default:
                return 0;
        }
    }
}
